<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","nobkt");
$PRIMARY_KEY    = PRIMARY_KEY;
$PRIMARY_VALUE  = GetParam($PRIMARY_KEY,"");
$FileName       = "ais_transaction_memoest.php";
$TableName		= "ais_memorial";
$PageSize       = "10";

if (GetParam("Action","")=="Close") {
	SetClosing ("txtCheck",$PageSize);
}

function SetClosing ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			//print "closing ".GetParam($sequence_name.$x,"")."<br>\n";
			if (!strlen(CheckClosing (GetParam($sequence_name.$x,"")))) ClosingEstimation (GetParam($sequence_name.$x,""));
	    }
	}
	header("Location: $FileName");
	exit;		
}

function ClosingEstimation ($nobkt) {
	global $DBConnection;
	$WHERE   = "WHERE nobkt='".$nobkt."' AND ktrasl='".GetOfficeID()."'";
	$SQL     = "SELECT DATE_FORMAT(tglbkt,'%d/%m/%Y') as tglbkt, jumlah,kdact, curr FROM ais_memorial ".$WHERE;
	$row     = $DBConnection->dbc->get_row($SQL);

//	$DBConnection->dbc->debug();

    $SystemDate = date("d/m/Y");
	$TransNo = SaveNumber("MR", $row->kdact, $SystemDate);


	$SQL = "SELECT rate FROM ais_kurssi WHERE tglbkt <= '".date("Y-m-d")."' AND curr='".$row->curr."' ORDER BY tglbkt DESC LIMIT 1";
	$rate = $DBConnection->dbc->get_var($SQL);

	$SQL     = "INSERT INTO ais_memorial (ledger, ktrasl, tglbkt, nobkt, urut, noref, skada, debtur, debnam, untush, uraian, uraian_a, curr, jumlah, dk, rate, rupiah, princ, vessel, voyage, kdact) ".
			   "SELECT ledger, ktrasl, '".date("Y-m-d")."', '".$TransNo."', urut, '".$nobkt."', skada, debtur, debnam, untush, uraian, uraian_a, curr, jumlah, IF(dk='D','K','D'), '$rate', $rate*jumlah, princ, vessel, voyage,kdact ".
			   "FROM ais_memorial ".
			   $WHERE;
			   
	$DBConnection->dbc->query($SQL);

    $SQL = "INSERT INTO ais_hutang (ledger,ktrasl,tglbkt,nobkt,urut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt,kdact) 
            SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','2','1'),kdact FROM ais_memorial WHERE (skada like '3%' OR skada like '4%') AND nobkt='".$TransNo."' ";
    $DBConnection->dbc->query($SQL);

    $SQL = "INSERT INTO ais_piutang (ledger,ktrasl,tglbkt,nobkt,normut,noref,nobkta,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,srt,kdact) 
            SELECT ledger,ktrasl,tglbkt,nobkt,urut,noref,noref,skada,debtur,debnam,uraian,curr,jumlah,dk,rate,rupiah,if(dk='D','1','2'),kdact FROM ais_memorial WHERE (skada like '0006%' OR skada like '0007%') AND nobkt='".$TransNo."' ";
    $DBConnection->dbc->query($SQL);

	$SQL = "UPDATE ais_memorial_history SET memoclose = '$TransNo', jml_close='".$row->jumlah."' WHERE memoesti='".$nobkt."'";
	$DBConnection->dbc->query($SQL);
	SetLogActivity($TransNo,"Create","");
}

function CheckClosing ($nobkt) {
	global $DBConnection;
	$WHERE   = "WHERE memoesti='".$nobkt."' AND ktrasl='".GetOfficeID()."'";
	$SQL     = "SELECT memoclose FROM ais_memorial_history ".$WHERE;
	$isExist = $DBConnection->dbc->get_var($SQL);
	return $isExist;
}

?>
